<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mon extends CI_Controller {

    // num of records per page
    private $limit = 10;

    function __construct()
    {
        parent::__construct();

        // load library
        //$this->load->library(array('table','form_validation'));
        $this->load->helper('url');

        // load model
        $this->load->model('admin/Mon_model','',TRUE);

        $this->is_logged_in();
        $this->isAdmin();
        $this->isTeacher();
    }
    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    function isAdmin(){
        $isAdmin = $this->session->userdata('isAdmin');
        if(!isset($isAdmin) || $isAdmin != 'isAdmin')
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    function isTeacher(){
        $isTeacher = $this->session->userdata('isTeacher');
        if($isTeacher == '' || $isTeacher != 'isTeacher')
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    function index($offset = 0)
    {
        // offset
        if($this->isAdmin()){
            $uri_segment = 3;
            $offset = $this->uri->segment($uri_segment);

            // load data
            $mons = $this->Mon_model->get_paged_list($this->limit, $offset)->result();

            // generate pagination
            $this->load->library('pagination');
            $config['base_url'] = site_url('admin/mon/index/');
            $config['total_rows'] = $this->Mon_model->count_all();
            $config['per_page'] = $this->limit;
            $config['uri_segment'] = $uri_segment;
            $this->pagination->initialize($config);
            $data['pagination'] = $this->pagination->create_links();

            // generate table data
            $this->load->library('table');
            $this->table->set_empty("&nbsp;");
            $this->table->set_heading('STT', 'Id Môn', 'Ngành', 'Tên Môn','Tín Chỉ','State', 'Actions');
            $i = 0 + $offset;
            foreach ($mons as $mon)
            {
                $this->table->add_row(++$i, $mon->id,$mon->nganh,$mon->ten_mon,$mon->tinchi,$mon->state,
                    anchor('admin/mon/view/'.$mon->id,'view',array('class'=>'view')).' '.
                        anchor('admin/mon/update/'.$mon->id,'update',array('class'=>'update')).' '.
                        anchor('admin/mon/delete/'.$mon->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Bạn có chắc xóa môn học này ?')"))
                );
            }
            $data['table'] = $this->table->generate();

            // load view
            $this->load->view('admin/mon/monList', $data);
        }else{
            echo "Bạn không có quyền này!!";
        }
    }

    function add()
    {
        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // set common properties
        $data['title'] = 'Thêm Môn Học';
        $data['message'] = '';
        $data['action'] = site_url('admin/mon/addMon');
        $data['link_back'] = anchor('admin/mon/index/','Trở lại Danh sách Môn',array('class'=>'back'));

        // load view
        $this->load->view('admin/mon/monEdit', $data);
    }

    function addMon()
    {
        // set common properties
        $data['title'] = 'Thêm Môn Học';
        $data['action'] = site_url('admin/mon/addMon');
        $data['link_back'] = anchor('admin/mon/index/','Trở lại Danh sách Môn',array('class'=>'back'));

        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // run validation
        if ($this->form_validation->run() == FALSE)
        {
            $data['message'] = '';
        }
        else
        {
            // save data
            $mon = array('nganh' => $this->input->post('nganh'),
                'ten_mon' => $this->input->post('ten_mon'),
                'tinchi' => $this->input->post('tinchi'),
                'state'=>$this->input->post('state')

            );
            //$id = $this->Mon_model->save($user);
            $id =$this->Mon_model->save($mon);

            // set user message
            $data['message'] = '<div class="success">Thêm Môn Học Thành Công</div>';
        }

        // load view
        $this->load->view('admin/mon/monEdit', $data);
    }

    function view($id)
    {
        // set common properties
        $data['title'] = 'Account Details';
        $data['link_back'] = anchor('admin/mon/index/','Trở lại Danh sách Môn',array('class'=>'back'));

        // get person details
        $data['mon'] = $this->Mon_model->get_by_id($id)->row();

        // load view
        $this->load->view('admin/mon/monView', $data);
    }

    function update($id)
    {
        // set validation properties
        $this->_set_rules();

        // prefill form values
        $mon = $this->Mon_model->get_by_id($id)->row();
        $this->form_data->id = $id;
        $this->form_data->nganh = $mon->nganh;
        $this->form_data->ten_mon = $mon->ten_mon;
        $this->form_data->tinchi= $mon->tinchi;
        $this->form_data->state= $mon->state;
               // set common properties
        $data['title'] = 'Cập Nhật Môn Học ';
        $data['message'] = '';
        $data['action'] = site_url('admin/mon/updateMon');
        $data['link_back'] = anchor('admin/mon/index/','Trở lại Danh sách Môn',array('class'=>'back'));

        // load view
        $this->load->view('admin/mon/monEdit', $data);
    }

    function updateMon()
    {
        // set common properties
        $data['title'] = 'Update account';
        $data['action'] = site_url('admin/mon/updateMon');
        $data['link_back'] = anchor('admin/mon/index/','Trở lại Danh sách Môn',array('class'=>'back'));

        // set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();

        // run validation
        if ($this->form_validation->run() == FALSE)
        {
            $data['message'] = '';
        }
        else
        {
            // save data
            $id = $this->input->post('id');
            $mon = array('nganh' => $this->input->post('nganh'),
                'ten_mon' => $this->input->post('ten_mon'),
                'tinchi' => $this->input->post('tinchi'),
                'state'=>$this->input->post('state')

            );

            $this->Mon_model->update($id,$mon);

            // set user message
            $data['message'] = '<div class="success">Cập Nhật Môn Học Thành Công</div>';
        }

        // load view
        $this->load->view('admin/mon/monEdit', $data);
    }

    function delete($id)
    {
        // delete person
        $this->Mon_model->delete($id);

        // redirect to person list page
        redirect('admin/mon/index/','refresh');
    }

    // set empty default form field values
    function _set_fields()
    {
        $this->form_data->id = '';
        $this->form_data->nganh = '';
        $this->form_data->ten_mon = '';
        $this->form_data->tinchi= '';
        $this->form_data->state= '';

    }

    // validation rules
    function _set_rules()
    {
        $this->form_validation->set_rules('nganh', 'nganh', 'trim|required');
        $this->form_validation->set_rules('ten_mon', 'ten_mon', 'trim|required');
        $this->form_validation->set_rules('tinchi', 'tinchi', 'trim|required');
        $this->form_validation->set_rules('state', 'state', 'trim|required');

        $this->form_validation->set_message('required', '* Vui lòng điền thông tin vào ');
        $this->form_validation->set_message('isset', '* required');
        $this->form_validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
    }

    // date_validation callback
    function valid_date($str)
    {
        //match the format of the date
        if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts))
        {
            //check weather the date is valid of not
            if(checkdate($parts[2],$parts[1],$parts[3]))
                return true;
            else
                return false;
        }
        else
            return false;
    }
}
?>